﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using prjKQXS_UI.Security;
using prjKQXS_DLL;


namespace prjKQXS_UI.Security
{
    public partial class frmWaiting : XtraForm
    {
        public frmWaiting()
        {
            InitializeComponent();
        }

        private void frmWaiting_Shown(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private bool showConfig = true;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                BienTc.ACent_dir = Application.StartupPath;
                if (File.Exists(BienTc.fileIni))  //Nếu có tệp cấu hình
                {
                    BienTc.connString = Config.loadConfig();
                    showConfig = !Config.testConnection(BienTc.connString);
                }
            }
            catch
            {
                //Cấu hình lại nếu gặp bất kỳ lỗi (showConfig = true theo mặc định);
            }
            finally
            {
                backgroundWorker1.CancelAsync();
                backgroundWorker1.Dispose();
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Hide();
            if (showConfig)
            {
                frmConfigDatabase config = new frmConfigDatabase(this.Name);
                config.ShowDialog();
            }
            else
            {
                frmLogin frm = new frmLogin();
                frm.ShowDialog();
            }
        }
    }
}